#include "pareto.h"

void Conf::print(){
        /*
        cout<<'{';
        for(int i=0; i<tracedSigs.size(); ++i)
                cout<<tracedSigs[i]->getName()<<',';
        cout<<"}=";

        cout<<'('<<TV<<','<<TVC<<','<<TVNC<<')'<<endl;
        */
        cout<<"current conf set................"<<endl;
        
        for(int i=0; i<tracedSigs.size(); ++i)
        {
                cout<<tracedSigs[i]->getName()<<"\t";
                cout<<"P0 "<<tracedSigs[i]->getP0()<<"\t";
                cout<<"P1 "<<tracedSigs[i]->getP1()<<"\t";
                cout<<"R0 "<<tracedSigs[i]->getR0()<<"\t";
                cout<<"R1 "<<tracedSigs[i]->getR1()<<"\t";
                cout<<"V0 "<<tracedSigs[i]->getV0()<<"\t";
                cout<<"V1 "<<tracedSigs[i]->getV1()<<"\t";
                cout<<endl;
        }        
}


bool Conf::dominate(Conf *c){
	if((TVC >= c->TVC) & (TVNC >= c->TVNC))
		return true;
	return false;
}


Confset::~Confset(){
        for(int i=0; i<confs.size(); ++i)
                delete confs[i];
}

	
void Confset::rank(){
        sort(confs.begin(), confs.end(), moreTV());
        //sort(confs.begin(),confs.end(),dominate());
}


Confset *Confset::min(){
        for(int i=0; i<confs.size(); i++ ){
                for(int j=i+1; j<confs.size(); j++)	
                {
                        if(confs[i]->dominate(confs[j])){
                                delete confs[j];
                                confs.erase(confs.begin()+j);
                                --j;
                        }
                        else
                                if(confs[j]->dominate(confs[i])){
                                        delete confs[i];
                                        confs.erase(confs.begin()+i);
                                        j=i;//later j++
                                }
                }
        }			
}

	
int Confset::maxTVC(){
        int m=0, n=0;
        for(int i=0; i<confs.size(); ++i)
                if(m<confs[i]->TVC){
                        m = confs[i]->TVC;
                        n = i;
                }
        return n;
}

	
int Confset::maxTVNC(){
        int m=0, n=0;
        for(int i=0; i<confs.size(); ++i)
                if(m<confs[i]->TVNC){
                        m = confs[i]->TVNC;
                        n = i;
                }
        return n;
}

	
int Confset::maxTV(){

        int n=0;
        double m=0.0;
        
        for(int i=0; i<confs.size(); ++i)
                if(m<confs[i]->TV){
                        m = confs[i]->TV;
                        n = i;
                }
        return n;
}


/*
int Confset::minTV(){
        
        double m = 1.0e20;
        
        sort(confs.begin(), confs.end(), lessTV());
        
        return 0;
}
*/

	
void Confset::print(){
        for(int i=0; i<confs.size(); ++i)
                confs[i]->print();
}


Confset *Confset::reduce(int n){
        sort(confs.begin(), confs.end(), moreTV());
        if(confs.size()<=n)
                return NULL;
        int mc,mn;
        bool test1=false;
        mc = maxTVC();
		
        if(mc>=n){
                test1 = true;
                swap(confs[n-1],confs[mc]);
        }
		
        mn = maxTVNC();
        if(mn>=n){
                int index = (test1 ? (n-2):(n-1));
                swap(confs[index],confs[mn]);
        }
		
        confs.erase(confs.begin()+n, confs.end());		
}


Confset *Confset::reduce(void){
        sort(confs.begin(), confs.end(), moreTV());
        while(confs.size()>1)
                confs.erase(confs.begin()+confs.size()-1);		
}
